<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="108px">
      <el-form-item label="考核计划名称" prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入考核计划名称" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="考核周期类型" prop="status">
        <el-select v-model="queryParams.cycleType" placeholder="考核周期类型" clearable style="width: 200px">
          <el-option v-for="dict in cycle_type" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Plus"  v-hasPermi="['system:plan:add']">
          <router-link :to="{name:'kaohejihuaBianji',params:{id:123}}" class="link-type">
            <span>新增</span>
          </router-link>
        </el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate"
          v-hasPermi="['system:plan:edit']">修改</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['system:plan:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="Download" @click="handleExport"
          v-hasPermi="['system:plan:export']">导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="planList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="考核计划名称" align="center" prop="name" />
      <el-table-column label="考核部门" align="center" prop="departmentId" />
      <el-table-column label="考核周期类型" align="center" prop="cycleType" />
      <el-table-column label="考核周期" align="center" prop="cycle" />
      <el-table-column label="考核状态" align="center" prop="status" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" :disabled="scope.row.status=='草稿'?false:true" @click="faqikaohe(scope.row)"
            v-hasPermi="['system:plan:edit']">发起考核</el-button>
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['system:plan:edit']">编辑</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)"
            v-hasPermi="['system:plan:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize" @pagination="getList" />

 
  </div>
</template>

<script setup name="Plan">
import { listPlan, getPlan, delPlan, addPlan, updatePlan,add } from "@/api/jixiao/plan"

const { proxy } = getCurrentInstance()
const { cycle_type } = proxy.useDict("cycle_type");
import { useRouter } from "vue-router"
const router = useRouter()

const planList = ref([])
const open = ref(false)
const activeName = ref('first')
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const title = ref("")

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    name: null,
    departmentId: null,
    cycleType: null,
    cycle: null,
    status: null,
    performanceaccessmentId: null,
    assessorId: null,
    startDate: null,
  },
  rules: {
    name: [
      { required: true, message: "考核计划名称不能为空", trigger: "blur" }
    ],
  }
})

const { queryParams, form, rules } = toRefs(data)

// 新增考核计划
const handleClick = (tab, event) => {
  console.log(tab, event)
}

/** 查询员工考核计划----Dmy列表 */
function getList() {
  loading.value = true
  listPlan(queryParams.value).then(response => {
    planList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

// 取消按钮
function cancel() {
  open.value = false
  reset()
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    name: null,
    departmentId: null,
    cycleType: null,
    cycle: null,
    status: null,
    performanceaccessmentId: null,
    assessorId: null,
    startDate: null,
    delFlag: null
  }
  proxy.resetForm("planRef")
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef")
  handleQuery()
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

/** 新增按钮操作 */
function handleAdd() {
  reset()
  open.value = true
  title.value = "添加考核计划"
}

// 发起考核按钮
function faqikaohe(row) {
  console.log(row)
  const a={
    id:row.id,
    status:'进行中'
  }
  console.log(a)
  updatePlan(a).then(response => {
    proxy.$modal.msgSuccess("发起考核成功")
    getList()
  })
  add({id:a.id}).then(response => {
    proxy.$modal.msgSuccess("发起考核成功")
    getList() 
  })

}

/** 修改按钮操作 */
function handleUpdate(row) {
  console.log(row)
  router.push({ 
    name: 'kaohejihuaBianji',
     query: { 
      id: row.id ,
      groupName: row.name,
      cycleType: row.cycleType,
      month:row.startDate,
      cycle:row.cycle,
      scope:row.departmentId,
      assessor:row.assessorId,
      description:row.performanceaccessmentId
     } 
    })
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["planRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updatePlan(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功")
          open.value = false
          getList()
        })
      } else {
        addPlan(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功")
          open.value = false
          getList()
        })
      }
    }
  })
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value
  proxy.$modal.confirm('是否确认删除员工考核计划----Dmy编号为"' + _ids + '"的数据项？').then(function () {
    return delPlan(_ids)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => { })
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('system/plan/export', {
    ...queryParams.value
  }, `plan_${new Date().getTime()}.xlsx`)
}

getList()
</script>
